Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude properties of type never when emitting model schemas #1127

Merged
merged 1 commit into from
Oct 5, 2022

Conversation

daviwil
Copy link
Contributor

@daviwil daviwil commented Oct 5, 2022

This came up while working on https://github.com/Azure/cadl-azure/pull/2022#discussion_r984714147:

In some library authoring cases, it would be nice if a templated type could have a property that can be discarded when the never type is passed in. For example:

model OperationStatus<TResult> {
  id: string;
  result?: TResult;
}

op getOperationStatus<TResult = never>(id: string): OperationStatus<TResult>;

This would enable spec authors to use getOperationStatus or getOperationStatus<MyResultType> without the need for two separate operation signatures. When the OperationStatus type is emitted in OpenAPI, the result property will be elided when never is used in the operation signature.

@azure-pipelines
Copy link

You can try these changes at https://cadlplayground.z22.web.core.windows.net/prs/1127/

Check the website changes at https://cadlwebsite.z1.web.core.windows.net/prs/1127/

@@ -1046,6 +1046,11 @@ function createOAPIEmitter(program: Program, options: ResolvedOpenAPI3EmitterOpt
continue;
}

if (isNeverType(prop.type)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if it would make sense to have this built at the checker level, like it doesn't even include the property in the model object then.

I guess the issue with it then is you can't project it if the property is not there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I think that's a good way to look at it. We might want to make the property have some other type in the future, so possibly better to leave it in at the checker level.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe open a design issue on this? It's also worth considering if failing that it would be good to put it in MetadataInfo.isPayloadProperty called just above. Upside, one fewer thing for an emitter that follows the (to-be-written, #1083) docs on metadata. Seems reasonable to say respond false to both isPayload and isMetadata for that. Speaking of which, do we need to handle @header something: never, etc.?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is fine, just some thoughts for the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I will open a design issue for it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just opened #1128

@daviwil daviwil merged commit a0bcbff into microsoft:main Oct 5, 2022
@daviwil daviwil deleted the skip-never branch October 5, 2022 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants